package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.entity.AlertRule;
import com.zzyl.mapper.AlertRoleMapper;
import com.zzyl.service.AlertRoleService;
import com.zzyl.vo.AlertRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlertRoleServiceImpl implements AlertRoleService {
    @Autowired
    AlertRoleMapper alertRoleMapper;
    @Override
    public void addRole(AlertRuleDto alertRuleDto) {
        AlertRule bean = BeanUtil.toBean(alertRuleDto, AlertRule.class);
        alertRoleMapper.insertRole(bean);
    }

    @Override
    public PageResponse<AlertRuleVo> page(Integer pageNum, Integer pageSize, String alertRuleName, String functionName, String productKey) {
        PageHelper.startPage(pageNum,pageSize);
        Page<AlertRuleVo> pageRules=alertRoleMapper.queryPage(alertRuleName,functionName,productKey);
        pageRules.getResult().forEach(pageRule->{
            String role=pageRule.getFunctionName()+pageRule.getOperator()+pageRule.getValue()+"持续"+pageRule.getDuration()+"个周期就报警";
            pageRule.setRules(role);
        });

        return PageResponse.of(pageRules,AlertRuleVo.class);
    }

    @Override
    public void deleteById(Integer id) {
        alertRoleMapper.deleteById(id);
    }

    @Override
    public AlertRuleVo getById(Integer id) {
        return alertRoleMapper.getById(id);
    }

    @Override
    public void updateById(AlertRuleDto alertRuleDto) {
        AlertRule bean = BeanUtil.toBean(alertRuleDto, AlertRule.class);
        alertRoleMapper.updateById(bean);
    }

    @Override
    public void setStatus(Integer id, Integer status) {
        AlertRule alertRule=new AlertRule();
        alertRule.setId(Long.valueOf(id));
        alertRule.setStatus(status);
        alertRoleMapper.updateById(alertRule);
    }
}
